#include <REGX52.H>
#include "Delay.h"//延时 头文件
#include "MatrixKey.h"//矩阵键盘键码 头文件 
#include "LCD1602.h"//显示屏 头文件

char KeyNumber;
unsigned long Password;//密码
int Count;//按的次数

void main()
{
	LCD_Init();
	LCD_ShowString(1,1,"Password:");
	while(1)
	{
		KeyNumber = MatrixKey();
		if(KeyNumber)
		{
			//---------------------------------------------
			if(KeyNumber < 10)
			{
				if(Count < 4)
				{
					Password *= 10;//密码左移一位
					Password += (KeyNumber%10);//获取一位密码
					Count++;
					LCD_ShowNum(2,1,Password,4);//更新密码显示
				}
				else//按的次数超过密码位
				{
					LCD_ShowString(1,12,"What");
					LCD_ShowString(2,1,"are you doing?");
				}
			}
			//---------------------------------------------
			if(10 == KeyNumber)//确认
			{
				if(1226 == Password)
				{
					LCD_ShowString(2,10,"Right!");
					Password = 0;
					Count = 0;
				}
				else
				{
					LCD_ShowString(2,10,"Error!");
					Password = 0;
					Count = 0;
				}	
				LCD_ShowNum(2,1,Password,4);
			}
			//---------------------------------------------
			if(11 == KeyNumber)//取消
			{
				Password = 0;
				Count = 0;
				LCD_ShowNum(2,1,Password,4);
			}
		}
	}
}









